package cn.moukou.sbzz.controller;


import cn.moukou.sbzz.pojo.BankProduct;
import cn.moukou.sbzz.pojo.EnterpriseProduct;
import cn.moukou.sbzz.service.BankProductService;
import cn.moukou.sbzz.util.JwtUtil;
import cn.moukou.sbzz.util.Resp;
import io.jsonwebtoken.SignatureException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sbzz/api")
public class BankProductController {

    @Autowired
    BankProductService bankProductService;

    @PostMapping("/bank/product/add")
    public Resp<String> pubBankProduct(HttpServletRequest request,
                                             @RequestParam("form") String form,
                                             @RequestParam("business") String business,
                                             @RequestParam("pool") String pool,
                                             @RequestParam("title") String title,
                                             @RequestParam("keyword") String keyword,
                                             @RequestParam("maxCap") int maxCap,
                                             @RequestParam("period") int period,
                                             @RequestParam("rate") int rate,
                                             @RequestParam("desc") String desc,
                                             @RequestParam("qualification") String qualification,
                                             @RequestParam("process") String process,
                                             @RequestParam("note") String note) throws Exception {
        System.out.println(desc);
        BankProduct product = new BankProduct();
        product.setBid(JwtUtil.getUid(request));

        product.setForm(form);
        product.setBusiness(business);
        product.setPool(pool);

        product.setTitle(title);
        product.setKeyword(keyword);

        product.setMaxCap(maxCap);
        product.setPeriod(period);
        product.setRate(rate);
        product.setDetail(desc);
        product.setQualification(qualification);
        product.setProcess(process);
        product.setNote(note);
        product.setStatus(0);
        int cnt = bankProductService.pubOneBankProduct(product);
        if(cnt > 0) {
            return Resp.ok("插入成功");
        } else {
            return Resp.fail("插入失败");
        }
    }

    @GetMapping("/bank/product/my")
    public Resp<List<BankProduct>> getMyBankProduct(HttpServletRequest request) throws Exception {
        String bid = JwtUtil.getUid(request);
        return Resp.ok(bankProductService.getMyBankProduct(bid));
    }

    @GetMapping("/bank/product/one")
    public Resp<BankProduct> getOneBankProduct(@RequestParam("id") String id) throws Exception {
        return Resp.ok(bankProductService.getOneProduct(id));
    }

    @GetMapping("/bank/product/unpass")
    public Resp<List<BankProduct>> getUnpassBankProduct() throws Exception {
        return Resp.ok(bankProductService.getUnpassProduct());
    }

    @GetMapping("/bank/product/list")
    public Resp<List<BankProduct>> getBankProductList() throws Exception {
        return Resp.ok(bankProductService.getProductList());
    }

    @GetMapping("/bank/product/filter")
    public Resp<List<BankProduct>> getBankProductByFilter(HttpServletRequest request, @RequestParam("form") String form, @RequestParam("business") String business, @RequestParam("pool") String pool) throws Exception {
        String token = request.getHeader("sbzz-token");
        String method = request.getMethod();
        if(StringUtils.isEmpty(token)){
            System.out.println(method);
            throw new SignatureException(JwtUtil.getHeader()+ "不能为空");
        }

        Map<String, Object> map = JwtUtil.getInfoFromToken(token);
        if(map.get("role").equals("admin") || map.get("role").equals("enterprise")) {
            return Resp.ok(bankProductService.getProductByFilter(form, business, pool));
        } else {
            return Resp.fail("无权限");
        }
    }

    @PostMapping("/bank/product/pass")
    public Resp<String> passBankProduct(@RequestParam("id") String id) throws Exception {
        if(bankProductService.passProduct(id) > 0) {
            return Resp.ok("审核成功");
        } else {
            return Resp.fail("审核失败");
        }
    }

}
